#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using starLiGHT.Collision;
using starLiGHT.Collision.BroadPhase;
using starLiGHT.Collision.NarrowPhase;
using System.Diagnostics;
using starLiGHT.Components.SandboxManager;
#endregion

namespace starLiGHT.Sandbox.Collision
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        #region Member Variables
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public SpriteFont descriptionFont;
        KeyboardState lastState;
        CollisionSystemTest test;
        #endregion

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;

            lastState = Keyboard.GetState();
        }

        protected override void Initialize()
        {
            SandboxManager.Initialize(typeof(Program).Assembly);

            test = SandboxManager.getActTest(this) as CollisionSystemTest;
            test.Initialize(this);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            descriptionFont = Content.Load<SpriteFont>("Description");
        }

        protected override void Update(GameTime gameTime)
        {
            KeyboardState state = Keyboard.GetState();

            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                state.IsKeyDown(Keys.Escape))
                this.Exit();

            if (state.IsKeyDown(Keys.OemPlus) && lastState.IsKeyUp(Keys.OemPlus))
            {
                test = SandboxManager.getNextTest(this) as CollisionSystemTest;
                test.Initialize(this);
            }

            if (state.IsKeyDown(Keys.OemMinus) && lastState.IsKeyUp(Keys.OemMinus))
            {
                test = SandboxManager.getPreviousTest(this) as CollisionSystemTest;
                test.Initialize(this);
            }

            test.Update(gameTime, state, lastState);
            base.Update(gameTime);

            lastState = state;
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            test.Draw(gameTime, spriteBatch);

            int line = 0;

            spriteBatch.Begin();
            spriteBatch.DrawString(descriptionFont, test.Description, new Vector2(0, (line++ * descriptionFont.LineSpacing)), Color.White);
            spriteBatch.DrawString(descriptionFont, "BroadPhase: " + (test.CollisionManager.BroadPhase != null ? test.CollisionManager.BroadPhase.GetType().Name : "<none>"), new Vector2(0, (line++ * descriptionFont.LineSpacing)), Color.LightGray);
            spriteBatch.DrawString(descriptionFont, "  Test count: " + (test.CollisionManager.BroadPhase != null ? test.CollisionManager.BroadPhase.TestCount.ToString() : "0"), new Vector2(0, (line++ * descriptionFont.LineSpacing)), Color.LightGray);
            spriteBatch.DrawString(descriptionFont, "  Pairs: " + test.CollisionManager.PairManager.Count.ToString(), new Vector2(0, (line++ * descriptionFont.LineSpacing)), Color.LightGray);

            spriteBatch.DrawString(descriptionFont, "NarrowPhase: " + test.CollisionManager.NarrowPhase.GetType().Name, new Vector2(0, (line++ * descriptionFont.LineSpacing)), Color.LightGray);
            spriteBatch.DrawString(descriptionFont, "  Collisions: " + test.CollisionManager.NarrowPhase.CollisionCount.ToString(), new Vector2(0, (line++ * descriptionFont.LineSpacing)), Color.LightGray);
            
            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
